﻿using KayakMes.Common.Dtos.WarehouseManageDto.Input;
using KayakMes.Common.Result;
using KayakMes.WarehouseManage.Application;
using KayakMes.WarehouseManage.Domain.Entities;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace KayakMes.WarehouseManage.Api.Controllers
{
    /// <summary>
    /// 采购订单控制器
    /// 提供采购订单相关的HTTP API接口
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PurchaseOrderController : ControllerBase
    {
        /// <summary>
        /// 仓库管理服务接口
        /// </summary>
        private readonly IWarehouseManageServices warehouseManageServices;

        /// <summary>
        /// 构造函数，依赖注入仓库管理服务
        /// </summary>
        /// <param name="warehouseManageServices">仓库管理服务实例</param>
        public PurchaseOrderController(IWarehouseManageServices warehouseManageServices)
        {
            this.warehouseManageServices = warehouseManageServices;
        }
        /// <summary>
        /// 添加采购订单
        /// </summary>
        /// <param name="purchaseOrderModel">采购订单模型</param>
        /// <returns>返回操作结果，包含新增订单的ID</returns>
        [HttpPost]
        public ApiResult<int> AddPurchaseOrder(PurchaseOrderModel purchaseOrderModel)
        {
            return warehouseManageServices.AddPurchaseOrder(purchaseOrderModel);
        }

        /// <summary>
        /// 获取采购订单列表（支持分页和查询条件）
        /// </summary>
        /// <param name="dto">采购订单查询条件DTO</param>
        /// <returns>返回分页的采购订单列表</returns>
        [HttpGet]
        public ApiPaging<PurchaseOrderModel> GetAllPurchaseOrders([FromQuery]PurchaseOrderDto dto)
        {
            return warehouseManageServices.GetAllPurchaseOrders(dto);
        }

        /// <summary>
        /// 根据ID获取指定的采购订单
        /// </summary>
        /// <param name="id">采购订单ID</param>
        /// <returns>返回操作结果，包含指定的采购订单信息</returns>
        [HttpGet]
        public ApiResult<PurchaseOrderModel> GetPurchaseOrder(long id)
        {
            return warehouseManageServices.GetPurchaseOrder(id);
        }

        /// <summary>
        /// 更新采购订单信息
        /// </summary>
        /// <param name="purchaseOrderModel">采购订单模型</param>
        /// <returns>返回操作结果，包含更新操作的影响行数</returns>
        [HttpPut]
        public ApiResult<int> UpdatePurchaseOrder(PurchaseOrderModel purchaseOrderModel)
        {
            return warehouseManageServices.UpdatePurchaseOrder(purchaseOrderModel);
        }
        [HttpPut]
        /// <summary>
        /// 逻辑删除采购订单
        /// 将订单的IsDeleted字段设置为true，而不是物理删除
        /// </summary>
        /// <param name="Id">采购订单ID</param>
        /// <returns>返回操作结果，成功返回1，失败返回0</returns>
        public ApiResult<int> LogicDel(long Id)
        {
            return warehouseManageServices.LogicDel(Id);
        }
        [HttpGet]
        /// <summary>
        /// 随机生成检测项目编号
        /// </summary>
        /// <returns>格式为 PC001 的字符串</returns>
        public string GetCode()
        {
            return warehouseManageServices.GetCode();
        }

    }
}
